<template>
  <div class="reportMistakeInfoAction el-card is-always-shadow el-card__body">
    <avue-crud
      ref="crud"
      :option="option"
      :table-loading="listLoading"
      :data="tableData"
      :page.sync="page"
      :search.sync="searchForm"
      @search-change="handleFilter"
      @selection-change="selectionChange"
      @on-load="search"
      v-loading="isShowLoading" element-loading-text = "正在提交数据，请稍后..." element-loading-spinner = "el-icon-loading" 
    >
      <template slot-scope="scope" slot="lineCodeSearch">
        <el-select
          v-model="searchForm.lineCode"
          placeholder="请选择"
          style="width: 150px"
          @change="changeLine"
          filterable
          clearable
        >
          <el-option
            v-for="(item, index) in lineCodes"
            :key="index"
            :label="item.name"
            :value="item.code"
          >
          </el-option>
        </el-select>
      </template>
      <template slot-scope="scope" slot="stationCodeSearch">
        <el-select
          v-model="searchForm.stationCode"
          placeholder="请选择"
          style="width: 150px"
          clearable
          filterable
          @change="changeStation"
        >
          <el-option
            v-for="(item, index) in stationCodes"
            :key="index"
            :label="item.name"
            :value="item.code"
          >
          </el-option>
        </el-select>
      </template>
      <template slot-scope="scope" slot="conductorNameSearch">
        <el-select
          v-model="searchForm.conductorName"
          placeholder="请选择"
          style="width: 150px"
          clearable
          filterable
        >
          <el-option
            v-for="(item, index) in conductorNames"
            :key="index"
            :label="item.nikename"
            :value="item.nikename"
          >
          </el-option>
        </el-select>
      </template>

      <template slot="menuRight"
        ><el-button type="primary" size="mini" :disabled="submitLoading" @click="submitBomData()"
          >提交</el-button
        >
        <el-button type="primary" size="mini" @click="exportExcel" hidden
          >导出</el-button
        >
      </template>

      <template slot="searchMenu" slot-scope="{ row, size }">
        <el-button
          :size="size"
          @click="
            () => {
              setSearchFiledsEmpty(row);
              setSearchFiledsEmpty(searchForm);
            }
          "
          icon="el-icon-delete"
          >清空</el-button
        >
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button
          type="text"
          size="small"
          plain
          @click.stop="preview(scope.row)"
          >预览</el-button
        >
      </template>
    </avue-crud>

    <preview ref="preview"></preview>
  </div>
</template>


<script>
import preview from "./preview.vue";
import moment from "moment";
import {
  getgetStationCodes,
  getLineCodes,
  getSysUserToLS,
  getStationByCode,
  isLongPeriodSettlement,
  setSearchFiledsEmpty,
} from "../../../../api/subwayRat/commonCheck";
import { exportDataToExcel } from "../../../../api/subwayRat/exportSelf";
import {
  bomForSubmit,
  getBomNotConfirmDatas,
  getPreview,
  getReportBomNotConfirmDatas,
  getXw_bom_shouJieSuan_for_waitSubmit,
  waitSubmitMainOptionList,
} from "@/api/subwayRat/shouRuJieSuanBuTiJiao/bom/repairSubmitMain";
export default {
  components: { preview },
  data() {
    return {
      row: {},
      listLoading: true,
      fileTemp: {},
      searchForm: {
        lineCode: "",
        stationCode: "",
        conductorName: "",
        settleTime: "",
      },
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        current: 1,// 当前页数
        pageSize: 10, // 每页显示多少条,
        size: 10,// 每页显示多少条
        isAsc: false, // 是否倒序
      },
      autoWidth: true,
      downloadLoading: false,
      isShowLongPeriodSettlementBtn: false, //是否展示长周期结算按钮
      multipleSelectionRows: [],
      multipleSelectionKeys: [],
      option: waitSubmitMainOptionList(),
      attendancePerson: {
        attendancePersonId: "",
        attendancePersonJobNo: "",
        attendancePersonName: "",
      }, //值班站长
      lineCodes: [],
      stationCodes: [],
      conductorNames: [],
      submitLoading: false,
      isShowLoading: false
    };
  },
  created() {
    if (this.$route.query.row) {
      this.row = this.$route.query.row;
      debugger;
      if (!this.row.submitLineCode && !this.row.submitStationCode) {
        this.row.submitLineCode = this.row.lineCode;
        this.row.submitStationCode = this.row.stationCode;
      }
      this.addContent("添加", 0);
    }
    getXw_bom_shouJieSuan_for_waitSubmit(this.searchForm).then((res) => {
      this.tableData = res.data.data.records;
      this.listLoading = false;
      this.page.total = res.data.data.total;
    });
  },
  methods: {
    moment,
    setSearchFiledsEmpty,
    changeLine(v) {
      //改变线路时
      getgetStationCodes(v).then((res) => {
        this.stationCodes = res;
        this.searchForm.stationCode = "";
      });
    },
    changeStation(v) {
      let p = {
        line_code: this.searchForm.lineCode,
        station_code: v,
      };
      getSysUserToLS(p).then((response) => {
        this.conductorNames = response.data.data;
        this.searchForm.conductorName = "";
      });
    },
    async exportData() {
      // 导出
      this.searchForm.pageNo = 1;
      this.searchForm.pageSize = 1000000000;
      if (this.selectKeys.length > 0) {
        let newData = JSON.parse(JSON.stringify(this.selectKeys));
        exportDataToExcel(
          newData,
          this.option.column,
          "线网报表差错更改通知"
        );
      } else {
        let res = await getXw_bom_shouJieSuan_for_waitSubmit(this.searchForm);
        let data = res.data.data.records;
        exportDataToExcel(data, this.option.column, "线网报表差错更改通知");
      }
    },
    search() {
      this.listLoading = true;
      this.page.size = this.page.pageSize;
      this.page.current = this.page.currentPage;
      Object.assign(this.searchForm, this.page);
      getXw_bom_shouJieSuan_for_waitSubmit(this.searchForm).then((res) => {
        this.tableData = res.data.data.records;
        this.listLoading = false;
        this.page.total = res.data.data.total;
      });
    },
    submitBomData() {
      //提交
      if (this.multipleSelectionRows.length <= 0) {
        this.$message.warning("请选择需要提交的数据！");
        return;
      }
      this.$confirm("是否确认提交？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.submitLoading = true;
        this.isShowLoading = true;
        bomForSubmit({
          params: JSON.stringify(this.multipleSelectionRows)
        })
          .then((res) => {
            if (res.data.code == 0) {
              this.$message.success("操作成功");
              this.search();
              this.submitLoading = false;
              this.isShowLoading = false;
            } else {
              this.$message.error("操作失败");
              this.submitLoading = false;
              this.isShowLoading = false;
            }
            this.search();
          })
          .catch((error) => {
            this.search();
            this.submitLoading = false;
            this.isShowLoading = false;
          });
      });
    },
    preview(row) {
      this.$nextTick(() => {
        row = getPreview(row);
        this.$refs.preview.search(row);
      });
    },
    handleSelectionChange(val, a) {
      this.multipleSelectionRows = val;
      this.multipleSelectionKeys = [];
      for (let i = 0; i < this.multipleSelectionRows.length; i++) {
        this.multipleSelectionKeys.push(this.multipleSelectionRows[i].id);
      }
    },
    async exportExcel() {
      // 导出
      let p = {};
      Object.assign(p, this.searchForm);
      p.pageNo = 1;
      p.pageSize = 1000000000;
      p.size=1000000000;
      if (this.multipleSelectionRows.length > 0) {
        let newData = JSON.parse(JSON.stringify(this.multipleSelectionRows));
        exportDataToExcel(
          newData,
          this.option.column,
          "线网报表差错更改通知-客值"
        );
      } else {
        let res = await getXw_bom_shouJieSuan_for_waitSubmit(p);
        let data = res.data.data.records;
        exportDataToExcel(
          data,
          this.option.column,
          "线网报表差错更改通知-客值"
        );
      }
    },
    selectionChange(list) {
      this.multipleSelectionRows = list;
      this.multipleSelectionKeys = [];
      for (let i = 0; i < list.length; i++) {
        this.multipleSelectionKeys.push(list[i].id);
      }
    },
    handleFilter(params, done) {
      Object.assign(this.searchForm, params);
      this.search();
      done();
    },
  },
  mounted() {
    this.search();
    isLongPeriodSettlement({
      lineCode: this.$store.getters.userInfo.lineCode,
      stationCode: this.$store.getters.userInfo.stationCode,
    }).then((res) => {
      this.isShowLongPeriodSettlementBtn = res.data.data;
    });
    getLineCodes().then((res) => {
      //获取线路
      this.lineCodes = res;
    });
  },
};
</script>
<style lang="scss">
</style>
